Lab9 leafmap
Question 1: Creating an Interactive Map¶
In [1]:
Copied!
import leafmap
import leafmap
In [2]:
Copied!
m = leafmap.Map()
url = "https://nominatim.openstreetmap.org/search?format=json&q={s}"
m.add_search_control(url, placeholder="Search for a place", position="topright", zoom=6)
m
m = leafmap.Map()
url = "https://nominatim.openstreetmap.org/search?format=json&q={s}"
m.add_search_control(url, placeholder="Search for a place", position="topright", zoom=6)
m
Out[2]:
Question 2: Adding Map Legends¶
In [3]:
Copied!
map = leafmap.Map()
url = "https://services.terrascope.be/wms/v2?"
map.add_wms_layer(
url,
name="World Landcover",
opacity=0.8,
layers="WORLDCOVER_2021_MAP",
transparent=True,
)
map.add_legend(
builtin_legend="ESA_WorldCover",
)
map
map = leafmap.Map()
url = "https://services.terrascope.be/wms/v2?"
map.add_wms_layer(
url,
name="World Landcover",
opacity=0.8,
layers="WORLDCOVER_2021_MAP",
transparent=True,
)
map.add_legend(
builtin_legend="ESA_WorldCover",
)
map
Out[3]:
Question 3: Creating Marker Clusters¶
Create a marker cluster visualization from a GeoJSON file of building centroids¶
In [4]:
Copied!
map_1 = leafmap.Map(
basemap="Google Satellite", center=(47.653441, -117.597316), zoom=16
)
map_1
map_1 = leafmap.Map(
basemap="Google Satellite", center=(47.653441, -117.597316), zoom=16
)
map_1
Out[4]:
In [5]:
Copied!
import geopandas as gpd
import geopandas as gpd
In [6]:
Copied!
gdf = gpd.read_file(
"https://github.com/opengeos/datasets/releases/download/places/wa_building_centroids.geojson"
)
gdf.head()
gdf = gpd.read_file(
"https://github.com/opengeos/datasets/releases/download/places/wa_building_centroids.geojson"
)
gdf.head()
Out[6]:
| Class | Confidence | Shape_Leng | Shape_Area | geometry | |
|---|---|---|---|---|---|
| 0 | 1 | 28.905460 | 68.457069 | 292.568026 | POINT (-117.60109 47.65499) |
| 1 | 1 | 99.420196 | 97.152785 | 556.702899 | POINT (-117.59953 47.65533) |
| 2 | 1 | 95.450807 | 90.916993 | 492.940128 | POINT (-117.59991 47.65514) |
| 3 | 1 | 91.446453 | 85.645123 | 453.842109 | POINT (-117.59953 47.65575) |
| 4 | 1 | 90.172392 | 78.057638 | 380.403649 | POINT (-117.59989 47.65534) |
In [7]:
Copied!
# Extract longitude and latitude from geometry and append to gdf
gdf["longitude"] = gdf.geometry.x
gdf["latitude"] = gdf.geometry.y
gdf.head()
# Extract longitude and latitude from geometry and append to gdf
gdf["longitude"] = gdf.geometry.x
gdf["latitude"] = gdf.geometry.y
gdf.head()
Out[7]:
| Class | Confidence | Shape_Leng | Shape_Area | geometry | longitude | latitude | |
|---|---|---|---|---|---|---|---|
| 0 | 1 | 28.905460 | 68.457069 | 292.568026 | POINT (-117.60109 47.65499) | -117.601092 | 47.654993 |
| 1 | 1 | 99.420196 | 97.152785 | 556.702899 | POINT (-117.59953 47.65533) | -117.599525 | 47.655326 |
| 2 | 1 | 95.450807 | 90.916993 | 492.940128 | POINT (-117.59991 47.65514) | -117.599910 | 47.655143 |
| 3 | 1 | 91.446453 | 85.645123 | 453.842109 | POINT (-117.59953 47.65575) | -117.599532 | 47.655747 |
| 4 | 1 | 90.172392 | 78.057638 | 380.403649 | POINT (-117.59989 47.65534) | -117.599892 | 47.655336 |
In [8]:
Copied!
map_1.add_marker_cluster(
gdf,
x="longitude",
y="latitude",
layer_name="Building Centroids",
radius=5,
outline_color="red",
fill_color="yellow",
fill_opacity=0.8,
)
map_1.add_marker_cluster(
gdf,
x="longitude",
y="latitude",
layer_name="Building Centroids",
radius=5,
outline_color="red",
fill_color="yellow",
fill_opacity=0.8,
)
Create circle markers for each building centroid using the Map.add_circle_markers_from_xy() method¶
In [9]:
Copied!
map_2 = leafmap.Map(
basemap="Google Satellite", center=(47.653441, -117.597316), zoom=16
)
map_2 = leafmap.Map(
basemap="Google Satellite", center=(47.653441, -117.597316), zoom=16
)
In [10]:
Copied!
map_2.add_circle_markers_from_xy(
gdf,
x="longitude",
y="latitude",
radius=5,
color="red",
fill_color="yellow",
fill_opacity=0.8,
layer_name="Building Centroids",
zoom_to_layer=True,
)
map_2
map_2.add_circle_markers_from_xy(
gdf,
x="longitude",
y="latitude",
radius=5,
color="red",
fill_color="yellow",
fill_opacity=0.8,
layer_name="Building Centroids",
zoom_to_layer=True,
)
map_2
Out[10]:
Question 4: Visualizing Vector Data¶
Visualize the building polygons GeoJSON file and style it with:¶
In [11]:
Copied!
map_3 = leafmap.Map(basemap="Google Satellite")
data = "https://github.com/opengeos/datasets/releases/download/places/wa_overture_buildings.geojson"
style = {"color": "red", "weight": 1.5, "fillOpacity": 0}
map_3.add_vector(data, layer_name="Overture Buildings", style=style, zoom_to_layer=True)
map_3
map_3 = leafmap.Map(basemap="Google Satellite")
data = "https://github.com/opengeos/datasets/releases/download/places/wa_overture_buildings.geojson"
style = {"color": "red", "weight": 1.5, "fillOpacity": 0}
map_3.add_vector(data, layer_name="Overture Buildings", style=style, zoom_to_layer=True)
map_3
Out[11]:
Visualize the road polylines GeoJSON file and style it with:¶
In [12]:
Copied!
map_4 = leafmap.Map(basemap="Google Satellite")
data = "https://github.com/opengeos/datasets/releases/download/places/las_vegas_roads.geojson"
style = {"color": "red", "weight": 2, "fillOpacity": 0}
map_4.add_vector(data, layer_name="Las Vegas Roads", style=style, zoom_to_layer=True)
map_4
map_4 = leafmap.Map(basemap="Google Satellite")
data = "https://github.com/opengeos/datasets/releases/download/places/las_vegas_roads.geojson"
style = {"color": "red", "weight": 2, "fillOpacity": 0}
map_4.add_vector(data, layer_name="Las Vegas Roads", style=style, zoom_to_layer=True)
map_4
Out[12]:
Create a choropleth map of county areas in the US:¶
In [13]:
Copied!
url = "https://github.com/opengeos/datasets/releases/download/us/us_counties.geojson"
gdf = gpd.read_file(url)
map_5 = leafmap.Map(center=(37.5, -96), zoom=4)
map_5.add_data(
gdf,
column="CENSUSAREA",
scheme="Quantiles",
cmap="Blues",
legend_title="Census Area",
)
map_5
url = "https://github.com/opengeos/datasets/releases/download/us/us_counties.geojson"
gdf = gpd.read_file(url)
map_5 = leafmap.Map(center=(37.5, -96), zoom=4)
map_5.add_data(
gdf,
column="CENSUSAREA",
scheme="Quantiles",
cmap="Blues",
legend_title="Census Area",
)
map_5
Out[13]:
In [ ]:
Copied!
Exercise 5: Creating a Split Map¶
In [14]:
Copied!
Pre_event_imagery = (
"https://github.com/opengeos/datasets/releases/download/raster/Libya-2023-07-01.tif"
)
Post_event_imagery = (
"https://github.com/opengeos/datasets/releases/download/raster/Libya-2023-09-13.tif"
)
Pre_event_imagery = (
"https://github.com/opengeos/datasets/releases/download/raster/Libya-2023-07-01.tif"
)
Post_event_imagery = (
"https://github.com/opengeos/datasets/releases/download/raster/Libya-2023-09-13.tif"
)
In [15]:
Copied!
map_6 = leafmap.Map()
map_6.split_map(
left_layer=Pre_event_imagery,
right_layer=Post_event_imagery,
left_label="Pre-event Imagery",
right_label="Post-event Imagery",
)
map_6
map_6 = leafmap.Map()
map_6.split_map(
left_layer=Pre_event_imagery,
right_layer=Post_event_imagery,
left_label="Pre-event Imagery",
right_label="Post-event Imagery",
)
map_6
Out[15]: